import set from 'lodash/set';
import { path } from 'ramda';
import nestedToOneLevelPath from '@kuiper-helpers/nested-to-one-level-path';
import { gqcPermissionCheck } from '@kuiper/auth';
import escapeRegExp from '../helpers/escapeRegExp';

export default server => {
	const {
		app: {
			model: { SitePortrait },
		},
	} = server;

	server.methods.registerGraphQLSchema({
		name: 'SitePortrait',
		model: SitePortrait,
		queryFields: {
			One: 'findOne',
			Pagination: 'pagination',
			Many: 'findMany',
		},
		mutationFields: {},
		onPreAddFields(tc) {
			const {
				app: {
					graphqlResolvers,
					schema: { User, Job, Task },
				},
			} = server;

			tc.addRelation('user', {
				resolver: () => User.mongooseResolvers.dataLoader({ lean: true }),
				prepareArgs: {
					_id: source => source.uid,
				},
				projection: { uid: 1 },
			});

			tc.addRelation('result', {
				resolver: () => Task.mongooseResolvers.dataLoader({ lean: true }),
				prepareArgs: {
					_id: source => source.lastChangeId,
				},
				projection: { lastChangeId: 1 },
			});

			tc.addRelation('job', {
				resolver: () => Job.mongooseResolvers.dataLoader({ lean: true }),
				prepareArgs: {
					_id: source => source.jobId,
				},
				projection: { jobId: 1 },
			});

			graphqlResolvers.Query.SitePortraitOne = graphqlResolvers.Query.SitePortraitOne.withMiddlewares(
				[
					gqcPermissionCheck({
						required: 'cloud-monitor\x01listTask',
						selfOnlyArgsBuilder: (_source, args, context) => {
							const uid = path(
								['request', 'auth', 'credentials', 'uid'],
								context
							);
							set(args, ['filter', 'uid'], uid);
						},
					}),
				]
			);

			graphqlResolvers.Query.SitePortraitMany = graphqlResolvers.Query.SitePortraitMany.withMiddlewares(
				[
					gqcPermissionCheck({
						required: 'cloud-monitor\x01listTask',
						selfOnlyArgsBuilder: (_source, args, context) => {
							const uid = path(
								['request', 'auth', 'credentials', 'uid'],
								context
							);
							set(args, ['filter', 'uid'], uid);
						},
					}),
				]
			).addFilterArg({
				name: 'taskSettings',
				type: 'TaskTaskSettingsInput',
				description: 'target',
				query: (query, value) => {
					Object.assign(query, nestedToOneLevelPath(value, 'taskSettings'));
				},
			});

			graphqlResolvers.Query.SitePortraitPagination = graphqlResolvers.Query.SitePortraitPagination.withMiddlewares(
				[
					gqcPermissionCheck({
						required: 'cloud-monitor\x01listTask',
						selfOnlyArgsBuilder: (_source, args, context) => {
							const uid = path(
								['request', 'auth', 'credentials', 'uid'],
								context
							);
							set(args, ['filter', 'uid'], uid);
						},
					}),
				]
			)
				.addFilterArg({
					name: 'taskSettings',
					type: 'TaskTaskSettingsInput',
					description: 'target',
					query: (query, value) => {
						Object.assign(query, nestedToOneLevelPath(value, 'taskSettings'));
					},
				})
				.addFilterArg({
					name: 'targetSearch',
					type: 'String',
					description: '正则过滤: target',
					query: (query, value) => {
						query.target = {
							$regex: escapeRegExp(value.trim()),
							$options: 'i',
						};
					},
				});
		},
	});
};
